//
//  MainPublickTools.m
//  BuxProject
//
//  Created by zxh on 2022/3/9.
//

#import "MainPublickTools.h"
#import "MD5ChangeTool.h"
#define   APP_WIDTH        [UIScreen mainScreen].bounds.size.width

@implementation MainPublickTools
+ (BOOL) phoneNumLength:(NSString *)str{
    if (![self ObjectIsNull:str]) {
        if (str.length == 11) {
            return YES;
        } else {
            return NO;
        }
    }
    return NO;
}
+ (NSString *)obtainNowtimestamp:(double)time
{
    //设置时间格式
    NSDateFormatter * formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"mm分ss秒"];
    //将时间转换为字符串
    NSString *timeStr=[formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:time]];
    return timeStr;
}
+ (NSString *)toolsUnitConversiongMet:(NSString *)money{
    long long numberMoney = [money longLongValue];
    NSString *reMoney;
    if (numberMoney<1000000) {// <100w
        reMoney = [NSString stringWithFormat:@"%@",money];
    } else {//100w以上  保留两位小数
        reMoney = [NSString stringWithFormat:@"%lld.%lld%lld万",numberMoney/10000,numberMoney%10000/1000,numberMoney%1000/100];
    }
    return reMoney;
}
+ (NSString *)DateTransformationmate:(NSString *)timeformate timeString:(nonnull NSString *)timestr {
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:timeformate];
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timestr longLongValue]];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
    
}
+(UIViewController *)obtaincurrentController{
    UIWindow *window=[[UIApplication sharedApplication]keyWindow];
    if(window.windowLevel!=UIWindowLevelNormal){
        NSArray *windows=[[UIApplication sharedApplication]windows];
        for(UIWindow *tmpWindow in windows){
            if(tmpWindow.windowLevel==UIWindowLevelNormal){
                window=tmpWindow;
                break;
            }
        }
    }
    UIViewController* currentViewController=window.rootViewController;
    //
    while(true){
        if(currentViewController.presentedViewController){
            currentViewController=currentViewController.presentedViewController;
        }
        else if([currentViewController isKindOfClass:[UINavigationController class]]){
            UINavigationController* navigationController=(UINavigationController* )currentViewController;
            currentViewController=[navigationController.childViewControllers lastObject];
        }
        else if([currentViewController isKindOfClass:[UITabBarController class]]){
            UITabBarController* tabBarController=(UITabBarController* )currentViewController;
            currentViewController=tabBarController.selectedViewController;
        }
        else{
            NSUInteger childViewControllerCount=currentViewController.childViewControllers.count;
            if(childViewControllerCount > 0){
                UIViewController *lastViewController = currentViewController.childViewControllers.lastObject;
                
                NSString *vcName = NSStringFromClass([lastViewController class]);
                //判断某个controller
                if(![vcName isEqualToString:@"JXPagerListContainerViewController"] && ![vcName isEqualToString:@"JXCategoryListContainerViewController"])
                {
                    currentViewController=currentViewController.childViewControllers.lastObject;
                }
                return currentViewController;
            }
            else{
                return currentViewController;
            }
        }
    }
    return currentViewController;
}
+ (NSString *)obtainNowlongtimestamp
{
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval interval = [date timeIntervalSince1970];//(毫秒*1000)
    NSString *timeString = [NSString stringWithFormat:@"%.0f", interval];
    return timeString;
}

+ (BOOL) wholeSpace:(NSString *) contetn
{
    if (!contetn) {
        return true;
    } else {
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimedString = [contetn stringByTrimmingCharactersInSet:set];
        if ([trimedString length] == 0) {
            return true;
        } else {
            return false;
        }
    }
}

//时间戳转时间
+ (NSString *)timestamptransformTimeType:(NSString *)timestamp timeShowType:(NSString *)typeStr
{
    long long time=[timestamp longLongValue];
    //    如果服务器返回的是13位字符串，需要除以1000，否则显示不正确(13位其实代表的是毫秒，需要除以1000)
    //    long long time=[timeStr longLongValue] / 1000;
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:typeStr];
    NSString*timeString=[formatter stringFromDate:date];
    return timeString;
}
+ (NSString *)getCurrentTime
{
    //2.3.1 换成时间戳
    NSDate *currentDate = [NSDate date];
    long long timeInterVal = [currentDate timeIntervalSince1970] * 1000;
    NSString *timeNow = [NSString stringWithFormat:@"%lld",timeInterVal];
    return timeNow;
}
+ (NSString *)obtainNowtimestamp:(double)endTimestamp timeShowType:(NSString *)showType
{

    NSTimeInterval timer1 = [[MainPublickTools getCurrentTime] doubleValue]/1000;
    NSTimeInterval timer2 = endTimestamp;
        
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:showType];
        
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:timer1];
        
    NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:timer2];
        
    // 日历对象（方便比较两个日期之间的差距）
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *cmps = [calendar components:unit fromDate:date toDate:date2 options:0];
    
    NSString *hour;
    NSString *minute;
    NSString *second;
    if (cmps.hour < 10) {
        hour = [NSString stringWithFormat:@"0%ld",(long)cmps.hour];
    } else {
        hour = [NSString stringWithFormat:@"%ld",(long)cmps.hour];
    }
    if (cmps.minute < 10) {
        minute = [NSString stringWithFormat:@"0%ld",(long)cmps.minute];
    } else {
        minute = [NSString stringWithFormat:@"%ld",(long)cmps.minute];
    }
    if (cmps.second < 10) {
        second = [NSString stringWithFormat:@"0%ld",(long)cmps.second];
    } else {
        second = [NSString stringWithFormat:@"%ld",(long)cmps.second];
    }

    NSString *dateString;
    if (cmps.hour > 0) {

        dateString = [NSString stringWithFormat:@"%@:%@:%@",hour, minute,second];
        
    } else if (cmps.minute > 0) {
        dateString = [NSString stringWithFormat:@"%@:%@",minute,second];
    } else if (cmps.second > 0) {
        dateString = [NSString stringWithFormat:@"00:%@",second];
    } else {
        dateString = @"00:00";
    }
    return dateString;
}
+(NSString *)objectChangeJsonWithObj:(id)object
{
    NSString *jsonStr= @"";
    NSData *data=[NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:nil];
    jsonStr=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonStr];
    NSRange range = {0,jsonStr.length};
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    NSRange range2 = {0,mutStr.length};
    //delete \n
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    return mutStr;
}
//时间戳转时间
+ (NSString *)starChangeToTime:(NSString *)timeStr showType:(NSString *)typeStr
{

    long long time=[timeStr longLongValue];
    //    如果服务器返回的是13位字符串，需要除以1000，否则显示不正确(13位其实代表的是毫秒，需要除以1000)
    //    long long time=[timeStr longLongValue] / 1000;

    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:time];

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

    [formatter setDateFormat:typeStr];

    NSString*timeString=[formatter stringFromDate:date];

    return timeString;

}
//
+(NSString *)getNowwsTime
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *datenow = [NSDate date];
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    NSArray *array = [currentTimeString componentsSeparatedByString:@"-"];
    NSInteger year = [array[0] integerValue];
    NSString *new = [NSString stringWithFormat:@"%ld-%@-%@",year-18,array[1],array[2]];
    return new;
}
+(NSString *)transformToJsonWithObj:(id)object{

    NSString *jsonStr= @"";

    NSData *data=[NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingSortedKeys error:nil];
    jsonStr=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

    NSMutableString *mutStr = [NSMutableString stringWithString:jsonStr];
    NSRange range = {0,jsonStr.length};
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    NSRange range2 = {0,mutStr.length};
    //去掉字符串中的换行符
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];

    return mutStr;

}

/** 将字典转换成json格式字符串,不含\n这些符号*/
+ (NSData *)changeFormatDataForDictionary:(NSDictionary *)dicJson
{
    if (![dicJson isKindOfClass:[NSDictionary class]]) {
        return nil;
    }
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dicJson options:0 error:nil];
    if (![jsonData isKindOfClass:[NSData class]]) {
        return nil;
    }
    return jsonData;
}
#pragma mark json转数组
+ (NSArray *)stringToJSON:(NSString *)jsonStr {
    if (jsonStr) {
        id tmp = [NSJSONSerialization JSONObjectWithData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments | NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:nil];
         
        if (tmp) {
            if ([tmp isKindOfClass:[NSArray class]]) {
                 
                return tmp;
                 
            } else if([tmp isKindOfClass:[NSString class]]
                      || [tmp isKindOfClass:[NSDictionary class]]) {
                 
                return [NSArray arrayWithObject:tmp];
                 
            } else {
                return nil;
            }
        } else {
            return nil;
        }
         
    } else {
        return nil;
    }
}
#pragma mark 根据文字内容及大小来获取宽度
+(CGFloat)getContentWidthWidthTextStr:(NSString *)textStr andTextFont:(UIFont *)textFont
{
    CGSize constraint1 = CGSizeMake(APP_WIDTH-20, 20000);
    CGSize size = [[NSString stringWithFormat:@"%@",textStr ] boundingRectWithSize:constraint1 options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:textFont}context:nil].size;
    return size.width;
}

#pragma mark 根据文字内容获取高度
+(CGFloat)getHeightTextString:(NSString *)textStr andTextFont:(UIFont *)textFont andWidth:(float)width
{
    CGSize constraint = CGSizeMake(width, MAXFLOAT);
    CGSize size = [[NSString stringWithFormat:@"%@",textStr] boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName:textFont} context:nil].size;
    return size.height;
}
#pragma mark 根据文字内容获取高度
+(CGFloat)getHeightTextString:(NSString *)textStr withTextFont:(UIFont *)textFont withWidth:(float)width withStyle:(NSMutableParagraphStyle *)style
{
    CGSize constraint = CGSizeMake(width, MAXFLOAT);
    CGSize size = [[NSString stringWithFormat:@"%@",textStr] boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName:textFont,NSParagraphStyleAttributeName:style} context:nil].size;
    return size.height;
}
#pragma mark 时间字符串转时间
+(NSDate *)dateFromString:(NSString *)timeStr withDateType:(NSString *)type
{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:type];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];//解决8小时时间差问题
    NSDate *date = [dateFormatter dateFromString:timeStr];
    return date;
}
#pragma mark json转Dic
+ (NSDictionary *)getDictWithJSON:(NSString *)jsonString
{
    if (jsonString == nil) {
        return nil;
    }

    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err)
    {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

/*!
 *时间转时间戳
 */
+ (NSString *)transformTimeToTSWithStr:(NSString *)time
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    NSDate *datenow = [formatter dateFromString:time];
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
    return timeSp;
}

//MainPublickTools
///是否是同一天
+ (BOOL)isidenticalTwoD:(NSString *)age{
    if(![self ObjectIsNull:age]){
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        NSDate *datenow = [NSDate date];
        NSString *currentTimeString = [formatter stringFromDate:datenow];
        //创建两个日期
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSDate *startDate = [dateFormatter dateFromString:age];
        NSDate *endDate = [dateFormatter dateFromString:currentTimeString];
        
        //利用NSCalendar比较日期的差异
        NSCalendar *calendar = [NSCalendar currentCalendar];
        
        NSCalendarUnit unit = NSCalendarUnitDay;//只比较天数差异
        //比较的结果是NSDateComponents类对象
        NSDateComponents *delta = [calendar components:unit fromDate:startDate toDate:endDate options:0];
        NSInteger day = delta.day;
        if(day != 0){
            return YES;
        }else{
            return NO;
        }
    }else{
        return YES;
    }
    
}
+(BOOL)ObjectIsNull:(NSObject *)obj{
    if([obj isKindOfClass:[NSString class]]){
        //字符串
        NSString *tmpObj = (NSString*)obj;
        NSCharacterSet *whiltSpace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *textStr = [tmpObj stringByTrimmingCharactersInSet:whiltSpace];
        if (textStr == nil ||
            [textStr isEqualToString:@""] ||
            textStr.length == 0 ||
            [textStr isKindOfClass:[NSNull class]] ||
            [textStr isEqualToString:@"(null)"]) {
            return YES;
        }
        {
            return NO;
        }
    }else if([obj isKindOfClass:[NSNull class]])
    {
        return YES;
    }else if(obj == nil)
    {
        return YES;
    }else if ([obj isKindOfClass:[NSArray class]]){
        if([(NSArray*)obj count]==0){
            return YES;
        }
    }else if ([obj isKindOfClass:[NSDictionary class]]){
        if([(NSDictionary*)obj count]==0){
            return YES;
        }
    }
    return NO;
}
+ (NSString *)obtainDeviceNameWithString:(NSString *)deviceName
{
    NSString *deviceString = nil;
    NSDictionary *deviceDict = @{@"iPhone3,1":@1,@"iPhone3,2":@2,@"iPhone3,3":@3,@"iPhone4,1":@4,@"iPhone5,1":@5,
                                 @"iPhone5,2":@6,@"iPhone5,3":@7,@"iPhone5,4":@8,@"iPhone6,1":@9,@"iPhone6,2":@10,
                                 @"iPhone7,1":@11,@"iPhone7,2":@12,@"iPhone8,1":@13,@"iPhone8,2":@14,@"iPhone8,4":@15,
                                 @"iPhone9,1":@16,@"iPhone9,3":@17,@"iPhone9,2":@18,@"iPhone9,4":@19,@"iPhone10,1":@20,
                                 @"iPhone10,4":@21,@"iPhone10,2":@22,@"iPhone10,5":@23,@"iPhone10,3":@24,@"iPhone10,6":@25,
                                 @"i386":@26,@"x86_64":@27,
                                 @"iPhone11,2":@28,@"iPhone11,4":@29,@"iPhone11,6":@30,@"iPhone11,8":@31,@"iPhone12,1":@32,
                                 @"iPhone12,3":@33,@"iPhone12,5":@34,@"iPhone12,8":@35,@"iPhone13,1":@36,@"iPhone13,2":@37,
                                 @"iPhone13,3":@38,@"iPhone13,4":@39,@"Phone14,4":@40,@"iPhone14,5":@41,@"iPhone14,2":@42,
                                 @"iPhone14,3":@43,
                                 
                                 
                                 
                                 @"iPod1,1":@201,@"iPod2,1":@202,@"iPod3,1":@203,@"iPod4,1":@204,@"iPod5,1":@205,
                                 @"iPod7,1":@206,@"iPod9,1":@207,@"iPad1,1":@208,@"iPad1,2":@209,@"iPad2,1":@210,
                                 @"iPad2,2":@211,@"iPad2,3":@212,@"iPad2,4":@213,@"iPad2,5":@214,@"iPad2,6":@215,
                                 @"iPad2,7":@216,@"iPad3,1":@217,@"iPad3,2":@218,@"iPad3,3":@219,@"iPad3,4":@220,
                                 @"iPad3,5":@221,@"iPad3,6":@222,@"iPad4,1":@223,@"iPad4,2":@224,@"iPad4,3":@225,
                                 @"iPad4,4":@226,@"iPad4,5":@227,@"iPad4,6":@228,@"iPad4,7":@229,@"iPad4,8":@230,
                                 @"iPad4,9":@231,@"iPad5,1":@232,@"iPad5,2":@233,@"iPad5,3":@234,@"iPad5,4":@235,
                                 @"iPad6,3":@236,@"iPad6,4":@237,@"iPad6,7":@238,@"iPad6,8":@239,@"iPad6,11":@240,
                                 @"iPad6,12":@241,@"iPad7,1":@242,@"iPad7,2":@243,@"iPad7,3":@244,@"iPad7,4":@245,
                                 @"iPad7,5":@246,@"iPad7,6":@247,@"iPad7,11":@248,@"iPad7,12":@249,@"iPad8,1":@250,
                                 @"iPad8,2":@251,@"iPad8,3":@252,@"iPad8,4":@253,@"iPad8,5":@254,@"iPad8,6":@255,
                                 @"iPad8,7":@256,@"iPad8,8":@257,@"iPad8,9":@258,@"iPad8,10":@259,@"iPad8,11":@260,
                                 @"iPad8,12":@261,@"iPad11,1":@262,@"iPad11,2":@263,@"iPad11,3":@264,@"iPad11,4":@265,
                                 @"iPad11,6":@266,@"iPad11,7":@267,@"iPad13,1":@268,@"iPad13,2":@269,@"iPad12,1":@270,
                                 @"iPad12,2":@271,@"iPad14,1":@272,@"iPad14,2":@273,@"iPad13,4":@274,@"iPad13,5":@275,
                                 @"iPad13,6":@276,@"iPad13,7":@277,@"iPad13,8":@278,@"iPad13,9":@279,@"iPad13,10":@280,
                                 @"iPad13,11":@281,
                                 
                                                                  
    };
    
    NSInteger type = [[deviceDict objectForKey:deviceName] integerValue];
    switch (type) {
        case 1:
        case 2:
        case 3:
            deviceString = @"iPhone 4";
            break;
        case 4:
            deviceString = @"iPhone 4S";
            break;
        case 5:
            deviceString = @"iPhone 5";
            break;
        case 6:
            deviceString = @"iPhone 5 (GSM+CDMA)";
            break;
        case 7:
            deviceString = @"iPhone 5c (GSM)";
            break;
        case 8:
            deviceString = @"iPhone 5c (GSM+CDMA)";
            break;
        case 9:
            deviceString = @"iPhone 5s (GSM)";
            break;
        case 10:
            deviceString = @"iPhone 5s (GSM+CDMA)";
            break;
        case 11:
            deviceString = @"iPhone 6 Plus";
            break;
        case 12:
            deviceString = @"iPhone 6";
            break;
        case 13:
            deviceString = @"iPhone 6s";
            break;
        case 14:
            deviceString = @"iPhone 6s Plus";
            break;
        case 15:
            deviceString = @"iPhone SE";
            break;
        case 16:
        case 17:
            deviceString = @"iPhone 7";
            break;
        case 18:
        case 19:
            deviceString = @"iPhone 7 Plus";
            break;
        case 20:
        case 21:
            deviceString = @"iPhone 8";
            break;
        case 22:
        case 23:
            deviceString = @"iPhone 8 Plus";
            break;
        case 24:
        case 25:
            deviceString = @"iPhone X";
            break;
        case 26:
        case 27:
            deviceString = @"Simulator";
            break;
        case 28:
            deviceString = @"iPhone XS";
            break;
        case 29:
            deviceString = @"iPhone XS Max";
            break;
        case 30:
            deviceString = @"iPhone XS Max";
            break;
        case 31:
            deviceString = @"iPhone XR";
            break;
        case 32:
            deviceString = @"iPhone 11";
            break;
        case 33:
            deviceString = @"iPhone 11 Pro";
            break;
        case 34:
            deviceString = @"iPhone 11 Pro Max";
            break;
        case 35:
            deviceString = @"iPhone SE 2";
            break;
        case 36:
            deviceString = @"iPhone 12 mini";
            break;
        case 37:
            deviceString = @"iPhone 12";
            break;
        case 38:
            deviceString = @"iPhone 12 Pro";
            break;
        case 39:
            deviceString = @"iPhone 12 Pro Max";
            break;
        case 40:
            deviceString = @"iPhone 13 mini";
            break;
        case 41:
            deviceString = @"iPhone 13";
            break;
        case 42:
            deviceString = @"iPhone 13 Pro";
            break;
        case 43:
            deviceString = @"iPhone 13 Pro Max";
            break;
        
            
        //iPod、iPad
        case 201:
            deviceString = @"iPod Touch 1G";
            break;
        case 202:
            deviceString = @"iPod Touch 2G";
            break;
        case 203:
            deviceString = @"iPod Touch 3G";
            break;
        case 204:
            deviceString = @"iPod Touch 4G";
            break;
        case 205:
            deviceString = @"iPod Touch 5G";
            break;
        case 206:
            deviceString = @"iPod Touch 6G";
            break;
        case 207:
            deviceString = @"iPod Touch 7G";
            break;
        case 208:
            deviceString = @"iPad";
            break;
        case 209:
            deviceString = @"iPad 3G";
            break;
        case 210:
        case 211:
        case 212:
        case 213:
            deviceString = @"iPad 2";
            break;
        case 214:
        case 215:
        case 216:
            deviceString = @"iPad Mini";
            break;
        case 217:
        case 218:
        case 219:
            deviceString = @"iPad 3";
            break;
        case 220:
        case 221:
        case 222:
            deviceString = @"iPad 4";
            break;
        case 223:
        case 224:
        case 225:
            deviceString = @"iPad Air";
            break;
        case 226:
        case 227:
        case 228:
            deviceString = @"iPad Mini 2";
            break;
        case 229:
        case 230:
        case 231:
            deviceString = @"iPad Mini 3";
            break;
        case 232:;
        case 233:
            deviceString = @"iPad Mini 4";
            break;
        case 234:
        case 235:
            deviceString = @"iPad Mini 4";
            break;
        case 236:
        case 237:
            deviceString = @"iPad Pro 9.7";
            break;
        case 238:
        case 239:
            deviceString = @"iPad Pro 12.9";
            break;
        case 240:
        case 241:
            deviceString = @"iPad 5";
            break;
        case 242:
        case 243:
            deviceString = @"iPad Pro 12.9 inch 2nd gen";
            break;
        case 244:
        case 245:
            deviceString = @"iPad Pro 10.5 inch";
            break;
        case 246:
        case 247:
            deviceString = @"iPad 6";
            break;
        case 248:
        case 249:
            deviceString = @"iPad 7";
            break;
        case 250:
        case 251:
        case 252:
        case 253:
            deviceString = @"iPad Pro 11-inch";
            break;
        case 254:
        case 255:
        case 256:
        case 257:
            deviceString = @"iPad Pro 12.9-inch 3rd gen";
            break;
        case 258:
        case 259:
            deviceString = @"iPad Pro 11-inch 2nd gen";
            break;
        case 260:
        case 261:
            deviceString = @"iPad Pro 12.9-inch 4th gen";
            break;
        case 262:
        case 263:
            deviceString = @"iPad Mini 5";
            break;
        case 264:
        case 265:
            deviceString = @"iPad Air 3";
            break;
        case 266:
        case 267:
            deviceString = @"iPad 8";
            break;
        case 268:
        case 269:
            deviceString = @"iPad Air 4";
            break;
        case 270:
        case 271:
            deviceString = @"iPad 9";
            break;
        case 272:
        case 273:
            deviceString = @"iPad Mini 6";
            break;
        case 274:
        case 275:
        case 276:
        case 277:
            deviceString = @"iPad Pro 11-inch 3nd gen";
            break;
        case 278:
        case 279:
        case 280:
        case 281:
            deviceString = @"iPad Pro 12.9-inch 5th gen";
            break;
        default:
            deviceString = deviceName;
            break;
    }
    return deviceString;
}
+(NSString *)StringIsSafe:(NSString *)contentStr
{
    if ([self ObjectIsNull:[NSString stringWithFormat:@"%@",contentStr]]) {
        return @"";
    }
    else
    {
        return [NSString stringWithFormat:@"%@",contentStr];
    }
}

+ (NSData*)obtainPhotoData:(UIImage*)image
{
    CGFloat scaleImage = 0;
    CGSize imageSize = [image size];
    CGFloat imageWidth = imageSize.width;
    CGFloat imageHeight = imageSize.height;
    CGSize imageSizeZoom = CGSizeZero;
    if (imageWidth < imageHeight) {
        scaleImage = imageHeight/1080;
    }else{
        scaleImage = imageWidth/1080;
    }
    imageSizeZoom = CGSizeMake(imageWidth/scaleImage, imageHeight/scaleImage);
    UIImage* imageZoom = [self toolchangeImageSize:image size:imageSizeZoom];
    
    NSUInteger dataLenthDefault = 614400;
    NSData* data = UIImageJPEGRepresentation(imageZoom, 1.0);
    
    if ([data length] > dataLenthDefault) {
        float scaleSize = (CGFloat)dataLenthDefault/(CGFloat)[data length];
        data = UIImageJPEGRepresentation(imageZoom, scaleSize);
    }
    
    return data;
}
+ (UIImage *)toolchangeImageSize:(UIImage *)img size:(CGSize)size{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    // 返回新的改变大小后的图片
    return scaledImage;
}

//汉字转拼音 是否支持全拼可选
+ (NSString *)transformToPinyin:(NSString *)aString isQuanPin:(BOOL)quanPin
{
    NSMutableString *str = [NSMutableString stringWithString:[self StringIsSafe:aString]];
    CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformMandarinLatin,NO);
    CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics,NO);
    NSArray *pinyinArray = [str componentsSeparatedByString:@" "];
    NSMutableString *allString = [NSMutableString new];
    
    if (quanPin)
    {
        int count = 0;
        for (int  i = 0; i < pinyinArray.count; i++)
        {
            for(int i = 0; i < pinyinArray.count;i++)
            {
                if (i == count) {
                    [allString appendString:@"#"];
                }
                [allString appendFormat:@"%@",pinyinArray[i]];
            }
            [allString appendString:@","];
            count ++;
        }
    }
    NSMutableString *initialStr = [NSMutableString new];
    //拼音首字母
    for (NSString *s in pinyinArray)
    {
        if (s.length > 0)
        {
            [initialStr appendString:[s substringToIndex:1]];
        }
    }
    [allString appendFormat:@"#%@",initialStr];
    [allString appendFormat:@",#%@",aString];
    return allString;
}

+ (void)textFieldEditingChanged:(UITextField *)textField withLength:(NSInteger)maxLength {
    
    NSString *toBeString = textField.text;
    NSString *lang = [[UIApplication sharedApplication]textInputMode].primaryLanguage; //ios7之前使用[UITextInputMode currentInputMode].primaryLanguage
    if ([lang isEqualToString:@"zh-Hans"]) { //中文输入
        UITextRange *selectedRange = [textField markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        if (!position) {// 没有高亮选择的字，则对已输入的文字进行字数统计和限制
            if (toBeString.length > maxLength) {
                textField.text = [toBeString substringToIndex:maxLength];
            }
        }
        else{//有高亮选择的字符串，则暂不对文字进行统计和限制
        }
    }else{//中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
        if (toBeString.length > maxLength) {
            textField.text = [toBeString substringToIndex:maxLength];
        }
    }
}
//图片渐变色
+ (UIImage *)gradientColorImageFromColors:(NSArray*)colors gradientType:(GradientType)gradientType imgSize:(CGSize)imgSize {
    NSMutableArray *ar = [NSMutableArray array];
    for(UIColor *c in colors) {
        [ar addObject:(id)c.CGColor];
    }
    UIGraphicsBeginImageContextWithOptions(imgSize, YES, 1);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL);
    CGPoint start;
    CGPoint end;
    switch (gradientType) {
        case GradientTypeTopToBottom:
            start = CGPointMake(0.0, 0.0);
            end = CGPointMake(0.0, imgSize.height);
            break;
        case GradientTypeLeftToRight:
            start = CGPointMake(0.0, 0.0);
            end = CGPointMake(imgSize.width, 0.0);
            break;
        case GradientTypeUpleftToLowright:
            start = CGPointMake(0.0, 0.0);
            end = CGPointMake(imgSize.width, imgSize.height);
            break;
        case GradientTypeUprightToLowleft:
            start = CGPointMake(imgSize.width, 0.0);
            end = CGPointMake(0.0, imgSize.height);
            break;
        default:
            break;
    }
    CGContextDrawLinearGradient(context, gradient, start, end,kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    CGGradientRelease(gradient);
    CGContextRestoreGState(context);
    CGColorSpaceRelease(colorSpace);
    UIGraphicsEndImageContext();
    return image;
}

+ (UIViewController *)findSuperViewController:(UIView *)sourceView
{
    id target=sourceView;
    while (target) {
        target = ((UIResponder *)target).nextResponder;
        if ([target isKindOfClass:[UIViewController class]]) {
            break;
        }
    }
    return target;
}
@end
